package com.openlab.service.moveCar.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.openlab.common.utils.Result;
import com.openlab.service.moveCar.dto.*;
import com.openlab.service.moveCar.eneity.MoveCar;
import com.openlab.service.moveCar.service.MoveCarService;
import com.openlab.service.moveCar.service.UserCarService;
import com.openlab.service.moveCar.service.WechatMpUserService;
import com.openlab.service.user.dto.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/move_car")
public class MoveCarController {
    String responsed_at;
    @Autowired
    MoveCarService moveCarService;

    @Autowired
    UserCarService userCarService;

    @Autowired
    WechatMpUserService wechatMpUserService;

    @PostMapping("/detail")
    public Result detailInfo(@RequestBody InfoParam infoParam) {
        Info detailInfo = moveCarService.detailInfo(infoParam.getId(), infoParam.getCommunity_id());

        Info info = Info.builder()
                .id(detailInfo.getId())
                .car_number(detailInfo.getCar_number())
                .move_reason(detailInfo.getMove_reason())
                .live_img(detailInfo.getLive_img())
                .subscribed(detailInfo.getSubscribed())
                .have_concat_info(detailInfo.getHave_concat_info())
                .response_content(detailInfo.getResponse_content())
                .created_at(detailInfo.getCreated_at())
                .wechat_mp_user_real_name(detailInfo.getWechat_mp_user_real_name())
                .wechat_mp_user_id(detailInfo.getWechat_mp_user_id())
                .property_company_user_id(detailInfo.getProperty_company_user_id())
                .property_company_user_real_name(detailInfo.getProperty_company_user_real_name())
                .build();
        ConcatList concatList = null;
        if (info.getHave_concat_info() == 1) {
            String carNumber = info.getCar_number();
            ConcatList list = userCarService.haveConcat(carNumber);
            concatList = ConcatList.builder()
                    .phone(list.getPhone())
                    .car_number(list.getCar_number()).build();
        }

        Map<String, Object> map = new HashMap<>();
        map.put("info", info);
        map.put("concatList", concatList);
        return Result.ok(200, map);
    }

    @PostMapping("/list")
    public Result list(@RequestBody ListParam listParam) {
        int page_num = listParam.getPage_num();
        int page_size = listParam.getPage_size();
        Long community_id = listParam.getCommunity_id();
        Boolean have_concat_info1 = listParam.getHave_concat_info();
        Boolean finish = listParam.getFinish();

        List<ListDto> list = moveCarService.getList(community_id);
        IPage<MoveCar> page = new Page<>(page_num, page_size);
        QueryWrapper<MoveCar> queryWrapper = new QueryWrapper<>();

        if (finish != null) {
            if (finish) {
                queryWrapper.eq("responsed_at", 1);
            } else {
                queryWrapper.eq("responsed_at", 0);
            }
        }

        if (have_concat_info1 != null) {
            if (have_concat_info1) {
                queryWrapper.eq("have_concat_info", 1);
            } else {
                queryWrapper.eq("have_concat_info", 0);
            }
        }
        queryWrapper.eq("community_id", community_id);
        queryWrapper.orderByDesc("id");

        moveCarService.page(page, queryWrapper);

        long total = page.getTotal();

        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("total", total);
        map.put("page_amount", Math.ceil(total / page_size));
        map.put("page_num", page_num);
        map.put("page_size", page_size);
        return Result.ok(200, map);
    }


    @PostMapping("/find")
    public Result find(@RequestBody FindParam findParam) {
        FindList serviceFindList = userCarService.getFindList(findParam.getCar_number());
        FindList findList = FindList.builder()
                .phone(serviceFindList.getPhone()).build();
        Map<String, Object> map = new HashMap<>();
        map.put("findList", findList);
        return Result.ok(200, map);
    }

    @PostMapping("/reply")
    public Result reply(@RequestBody DetailParam detailParam,
                        HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Detail detail = moveCarService.getDetail(detailParam.getId(), detailParam.getCommunity_id());
        if (detail == null || detail.getResponsedAt() == 1) {
            return Result.ok(-130, "非法挪车工单");
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        responsed_at = formatter.format(date);

        UpdateWrapper<MoveCar> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", detailParam.getId());
        updateWrapper.set("response_content", detail.getResponseContent());
        updateWrapper.set("responsed_at", responsed_at);

        return Result.ok(200, responsed_at);
    }


    @PostMapping("/detail/id={id}")
    public Result getDetail(@PathVariable String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("phrase1", "已处理");
        UserInfo userInfo = new UserInfo();
        map.put("thing2", userInfo.getReal_name());
        map.put("time3", responsed_at);
        return Result.ok(200, map);
    }
}
